package com.webex.ta.hydra.gui.action;

import com.webex.ta.hydra.gui.table.HydraTable;
import com.webex.ta.hydra.gui.GuiPackage;
import com.webex.ta.hydra.core.Command;
import com.webex.ta.hydra.Constants;
import com.webex.ta.hydra.clipboard.ClipBoard;

import java.awt.event.ActionEvent;
import java.util.Set;
import java.util.HashSet;
import java.util.List;
import java.util.ArrayList;

/**
 * Author: Vegaz
 * Date: 2010-10-28
 * Time: 11:02:02
 */
public class CopyCellAction implements ActionHandler {
    private static Set<String> commands = new HashSet<String>();

    static {
        commands.add(ActionNames.COPY);
    }

    public Set<String> getActionNames() {
        return commands;
    }

    public void doAction(ActionEvent e) {
        HydraTable table = GuiPackage.getInstance().getHydraTable();
        if (table.getSelectedColumn() < 0 && table.getSelectedRow() < 0) return;

        List<List<String>> cellValues = new ArrayList<List<String>>();
        for (int row :table.getSelectedRows()) {
            Command cmd = table.getModel().getCommandAt(row);
            List<String> columnValues = new ArrayList<String>();
            cellValues.add(columnValues);
            for (int col : table.getSelectedColumns()) {
                switch (col) {
                    case Constants.COLUMN_KEYWORD :
                        columnValues.add(cmd.getKeyword());
                        break;
                    case Constants.COLUMN_BREAKPOINT:
                        // do nothing
                        break;
                    default:
                        columnValues.add(cmd.getParameter(col -2 ));
                }
            }
        }
        ClipBoard.getInstance().setCellValues(cellValues);
    }
}
